<?php


/**
 * performs a session check to determine if a user is logged in. Requires session_start() before hand. 
 * If session is found, does nothing.
 * If session not found, redirects to login and exits.
 */
function md_session_check() {
	
if (!isset($_SESSION["user"]))	{		//variable not found. clear and send to login.
 	session_unset(); //so lets destroy whatever session there was and bring them to login page
	session_destroy();
	$url = "Location: login.php";
	$msg = "?msg=You must login before viewing that page.";
	header ( $url . $msg );
	exit();
  }	//end session check.
}

/**
 * Checks the server's request method for "POST" checks that the var you send is set in the POST AssocArray. 
 * @param string $varname Name of the var you want to check exists in POST.
 * @param string $referer Optional - Name of the page that should be submitting to this one. 
 * 		Note: the string "http://mike.delauter.net/" will be prepended to this variable.
 * @return true if everything's legit. False otherwise.
 */
function md_cgi_check($varname) {
	if ($_SERVER["REQUEST_METHOD"] != "POST") {
		md_log_error("request method was not post.");
		return false;
	}
	/*
	if ($referer != "") {
		if ("http://mike.delauter.net/" . $referer != $_SERVER["HTTP_REFERER"]) 
			return false;
	} 
*/
	if (!isset ($_POST[$varname]) ) {
		md_log_error("POST::$varname was not set when expected to be.");	
		return false;
	}
	
	return true;
}

/**
 * Writes an error message to the default custom error log OR to a file of your choosing.
 * @param string $text - The text to be written to the file. 
 * @param string $logfilename - Optional, the name of the logfile to write to. Default is md.err
 */
function md_log_error($text, $logfilename="md.err") {
	$ERRLOG = fopen(BASE . "/" . $logfilename , "a") or die("could not write to error log.");
	$text = date("y/m/d",time()) . " " . $text; 
	fwrite($ERRLOG, $text . "\n");
	fclose($ERRLOG);
}

/**
 * Writes an error message to the sql log file. 
 */
function md_sql_error($text)
{
	md_log_error( $text, 'sql.err');
}

/**
 * Includes a piece of jquery, specified by $str. 
 * Possible includes so far are 'lightbox', 'thickbox'.
 * @param string $plugin_name, the name of the jquery plugin to include.
 * @return True if found pluginname, False if not.
 */
function md_get_jquery_plugin($plugin_name) {
	$lcstr = strtolower($plugin_name);
	$base = BASE;
	if ($lcstr == "lightbox") {
		echo '<script type="text/javascript" src="' . $base . 'js/jquery.lightbox-0.5.js"></script>';
		echo '<link rel="stylesheet" type="text/css" href="' . $base . 'css/jquery.lightbox-0.5.css" media="screen" />';
	} else if ($lcstr == "thickbox") {
		 echo '<script type="text/javascript" src="' . $base . 'jquery/thickbox.js"></script>';
		 echo '<link rel="stylesheet" type="text/css" href="' . $base . 'css/thickbox.css" media="screen" />';
	}
	else {
		return false;
	}
	
	
	return true;
}